<template>
  <a-modal :title="title" :width="800" :visible="visible" :confirm-loading="confirmLoading" cancel-text="关闭" @ok="submit" @cancel="closeModal">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="病区名字">
          <a-input v-decorator="['name', validatorRules.val ]" placeholder="请输入病区名字" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="所属医院">
          <a-select placeholder="全部医院" v-decorator="['hospitalId', validatorRules.val ]">
            <a-select-option :value="item.val" v-for="(item, index) in hospitalUl" :key="index">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import hospitalList from '@/api/hospital/hospitalList';
import ApiTree from '@/api/hospital/areaList';
import { Slime } from '@/mixins/slime';
import { evaFiltersMixin } from '@/mixins/evaMixin';
export default {
  name: 'areaListModal',
  mixins: [Slime, evaFiltersMixin],
  data() {
    return {
      title: '编辑',
      form: this.$form.createForm(this),
      model: {},
      hospitalUl: [],
      validatorRules: {
        val: { rules: [{ required: true, message: '请输入内容!' }] },
      },
      id: 0,
    };
  },
  created() {
    this.initHospital();
  },
  methods: {
    initHospital() {
      const that = this;
      hospitalList.getList().then((res) => {
        that.hospitalUl = res.result.records.map((obj, i) => {
          return (obj = {
            name: obj.name,
            val: obj.id,
          });
        });
      });
    },
    // 增
    crudCreate() {
      this.crudUpdate({});
    },
    // 改
    crudUpdate(record) {
      this.form.resetFields(); //重新设置领域
      this.visible = true;
      if (!record.id) {
        return false;
      }
      ApiTree.getDetail(record.id)
        .then((res) => {
          let data = res.result;
          this.id = data.id;
          this.form.setFieldsValue({
            name: data.name,
            hospitalId: data.hospitalId,
          });
        })
        .finally(() => {});
    },
    // 提交
    submit() {
      const that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let param = values;
          if (!that.id) {
            that.submitCreate(param);
          } else {
            param = { ...param, id: that.id };
            that.submitUpdate(param);
          }
        }
      });
    },
    // 提交 增
    submitCreate(param) {
      const that = this;
      ApiTree.getAdd(param)
        .then((res) => {
          that.$message.success('新增成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 提交 改
    submitUpdate(param) {
      const that = this;
      ApiTree.getUpdate(param)
        .then((res) => {
          that.$message.success('编辑成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 弹窗-关闭
    closeModal() {
      this.confirmLoading = false;
      this.visible = false;
      this.id = null;
      this.form.resetFields();
      this.$emit('closeModal');
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>